package com.milci.ebusinesssuite;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 *  Copyright (C) 2007 Mehmet Milci
 *                 ebusinesssuite-dev@milci.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
public class eBusinessSuiteAuthenticator {

    private static final String GWUNAME = "APPLSYSPUB";
    private static final String GWUPASS = "PUB";
    private static final String FNDNAME = "APPS";

    private String dbstring;
    private String gwuname;
    private String gwupass;
    private String fndname;
    private String dbserverId;

    public eBusinessSuiteAuthenticator(String dbstring, String gwuname, String gwupass, String fndname, String dbserverId){
        this.dbstring = dbstring;
        if (gwuname==null) { 
          this.gwuname = GWUNAME;
        } else {
          this.gwuname = gwuname;
        }
        if (gwupass ==null) { 
          this.gwupass = GWUPASS;
        } else {
          this.gwupass = gwupass;
        }
        if (fndname ==null) { 
          this.fndname = FNDNAME;
        } else {
          this.fndname = fndname;
        }
        this.dbserverId = dbserverId;
    }

    public eBusinessSuiteAuthenticator(String dbstring) {
        this(dbstring,GWUNAME,GWUPASS,FNDNAME,null);
    }

    public boolean authenticate(String userId, String password) {
        eBusinessSuiteFndAuth accessRequest = new eBusinessSuiteFndAuth(dbstring,gwuname,gwupass,fndname,dbserverId);
        accessRequest.setUsername(userId);
        accessRequest.setPassword(password);
        return accessRequest.Authenticate();
    }

    public static void printUsage() {
        System.err.println("format is:");
        System.err.println("java com.milci.ebusinesssuite.eBusinessSuiteAuthenticator " +
                "[-u gwuname -p gwupass -f fndname -s dbserverId] dbstring");
        System.err.println(" defaults for gwuname, gwupass, dbserverId and fndname are " + GWUNAME +
        ", " + GWUPASS +  " ,null and " + FNDNAME);
    }

    public static void main(String[] args) {

        String dbstring = null;
        String gwuname = null;
        String gwupass = null;
        String fndname = null;
        String dbserverId= null;
        
        for (int i = 0; i < args.length;i++) {
            try {
                if (args[i].equalsIgnoreCase("-u")) {
                    gwuname = args[++i];
                } else if (args[i].equalsIgnoreCase("-p")) {
                    gwupass = args[++i];
                } else if (args[i].equalsIgnoreCase("-f")) {
                    fndname = args[++i];
                } else if (args[i].equalsIgnoreCase("-s")) {
                    dbserverId= args[++i];
                } else {
                    dbstring = args[i];
                }
            } catch (NumberFormatException e) {
                printUsage();
                System.exit(1);
            }
        }
        if (dbstring == null) {
            printUsage();
            System.exit(1);
        }
        try {
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
            eBusinessSuiteAuthenticator auth = new eBusinessSuiteAuthenticator(dbstring, gwuname, gwupass, fndname,dbserverId);
            System.out.print("Userid: ");
            String userid = inputReader.readLine();
            System.out.print("Password: ");
            String password = inputReader.readLine();
            boolean ok = auth.authenticate(userid,password);
            if (ok) {
                System.out.println("Authenticated!");
            } else {
                System.out.println("Rejected!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
